<?php
/**
 * 图片上传
 */
class Helper_File
{
//	public $files;
//	public $path;
    public static $singletonObjects = array();
         /** 
	 * 获取单例
	 * @return Helper_Img
	 * @throws AppException
	 */
    
        public static function getInstance()
	{
		$className = __CLASS__;
		if( !isset( self::$singletonObjects[$className] ) )
		{
			self::$singletonObjects[$className] = new self();
		}

		return self::$singletonObjects[$className];
	}
        /**
         *图片上传
         * @return string 
         */
	public function upload( $files , $path , $newwidth , $newheight)
	{
		if($files['error'] == 0)								 //判断文件名是否可以上传到服务器；
		{
			if(is_dir($path))								 //is_dir()判断给定的文件名是否是一个目录
			{
				if(is_uploaded_file($files['tmp_name']))		//判断文件上传是否符合http post
				{
				$stmp = explode(".",$files['name']);
				$name    = time().rand(0,1000000).".".$stmp[count($stmp)-1];
				$newname = $path."/".$name;

				if(move_uploaded_file($files['tmp_name'],$newname))       //上传到指定目录下
				{
						return $newname;
				}
			}
				else
				{
					echo "文件上传不合法";
				}
			}
			else
			{
				echo "上传文件目录不存在";
			}
		}
		else
		{
			switch($files['error'])
			{
				case 1 :
				echo "上传文件大小超出配置文件指令限定值";break;
				case 2 :
				echo "上传文件大小超出表单选项指定的值";break;
				case 3 :
				echo "文件上传不完整";break;
				case 4 :
				echo "没有上传任何文件";break;
			}
		}
	}
        //根据尺寸上传文件的大小
        public function uploadBySize( $files , $path , $newWidth , $newHeight  ){
            if($files['error'] == 0){
                if(is_dir($path)){
                    if(is_uploaded_file($files['tmp_name'])){
                            $uploadedfile = $files['tmp_name'];
                            $src = imagecreatefromjpeg($uploadedfile);
                            // 获得原图片的大小
                            list($width,$height)=getimagesize($uploadedfile);
                            //创建临时新图片
                            $tmp=imagecreatetruecolor($newWidth,$newHeight);
                            //创建新图片
                            imagecopyresampled($tmp,$src,0,0,0,0,$newWidth,$newHeight,$width,$height); 
                            
                            $stmp = explode(".",$files['name']);
                            $name    = time().rand(0,1000000).".".$stmp[count($stmp)-1];    
                            $filename = $path.'/'.$name;
                            imagejpeg($tmp,$filename,100);
                            return $name;
                            imagedestroy($src);
                            imagedestroy($tmp);
                            

                    }
                    else{
                        throw new AppException(8006);
                        
                    }
                    
                    
                }
                else{    
                    throw new AppException(8005);
                }
                
            }
            else{   
                if( $files['error'] ==1){
                    throw new AppException(8001);
                }
                elseif( $files['error'] ==2){
                    throw new AppException(8002);
                }
                elseif($files['error'] ==3){
                   throw new AppException(8003);  
                }
                else{
                     throw new AppException(8004);
                    
                }
            }
           
            
            
        }
        /**
         *多文件上传
         * @return type 
         */
	public function multiUp()
{
	$files_a = array();
	foreach($this->files as $key=>$value)
	{
	foreach($value as $keys=>$values)
		{
		$files_a[$keys][$key]=$values;
		}
	}
	$rs=array();
	foreach($files_a as $f)
	{
	$this->files=$f;
	$rs[]=$this->upload();
	}
	print_r($rs);
	return $rs;
}
public function del()
{
if(file_exists($this->path)."/".$this->files)
	{
	unlink($this->path."/".$this->files);
	clearstatcache();
	}

}

}

?>